package com.dongrj.framework.starter.sms.client;

import com.dongrj.framework.core.utils.JsonUtils;
import com.dongrj.framework.starter.sms.SmsRequest;
import com.dongrj.framework.starter.sms.SmsResponse;
import com.dongrj.framework.starter.sms.enums.SmsEnum;
import com.dongrj.framework.starter.sms.properties.HuaWeiYunSmsProperties;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.smsapi.v1.SMSApiClient;
import com.huaweicloud.sdk.smsapi.v1.SMSApiCredentials;
import com.huaweicloud.sdk.smsapi.v1.model.BatchSendSmsRequest;
import com.huaweicloud.sdk.smsapi.v1.model.BatchSendSmsRequestBody;
import com.huaweicloud.sdk.smsapi.v1.model.BatchSendSmsResponse;
import com.huaweicloud.sdk.smsapi.v1.model.SmsID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
public class HuaWeiYunSmsClient extends AbstractSmsClient {
    private final SMSApiClient client;
    private final HuaWeiYunSmsProperties huaWeiYunSmsProperties;

    public HuaWeiYunSmsClient(HuaWeiYunSmsProperties huaWeiYunSmsProperties) {
        this.huaWeiYunSmsProperties = huaWeiYunSmsProperties;
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        Region region = new Region("cn-north-4", new String[]{"https://smsapi.cn-north-4.myhuaweicloud.com:443"});
        String appKey = huaWeiYunSmsProperties.getAppKey();
        String appSecret = huaWeiYunSmsProperties.getAppSecret();
        SMSApiCredentials smsApiCredentials = new SMSApiCredentials().withAk(appKey).withSk(appSecret);
        this.client = SMSApiClient.newBuilder().withHttpConfig(config).withCredential(smsApiCredentials).withRegion(region).build();
    }

    @Override
    public SmsResponse doSend(SmsRequest smsRequest) {
        BatchSendSmsRequest batchSendSmsRequest = toBatchSendSmsRequest(smsRequest);
        BatchSendSmsResponse batchSendSmsResponse = client.batchSendSms(batchSendSmsRequest);
        List<SmsID> result = batchSendSmsResponse.getResult();
        SmsResponse smsResponse = SmsResponse.fail();
        if (CollectionUtils.isEmpty(result)) {
            log.info("doSend.batchSendSmsResponse={}", batchSendSmsResponse);
            return smsResponse;
        }
        List<SmsID> failItems = result.stream().filter(item -> notEquals(item.getStatus(), "000000")).collect(Collectors.toList());
        if (size(failItems) == 0) {
            smsResponse.setCode(1);
            return smsResponse;
        }
        log.info("doSend.batchSendSmsResponse={}", batchSendSmsResponse);
        return smsResponse;
    }

    private BatchSendSmsRequest toBatchSendSmsRequest(SmsRequest smsRequest) {
        BatchSendSmsRequest request = new BatchSendSmsRequest();
        Map<String, Object> templateMap = smsRequest.getTemplateMap();
        request.withBody(new BatchSendSmsRequestBody()
                .withFrom(huaWeiYunSmsProperties.getChannelCode())
                .withTo(smsRequest.getMobiles())
                .withTemplateId(smsRequest.getTemplateCode())
                .withTemplateParas(JsonUtils.toJson(templateMap))
                .withStatusCallback(huaWeiYunSmsProperties.getCallBackUrl()));
        return request;
    }

    @Override
    public SmsEnum smsEnum() {
        return SmsEnum.HuaWei;
    }
}
